package com.mumu.jetpackdemo.livedata;

import android.arch.core.util.Function;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Transformations;
import android.arch.lifecycle.ViewModel;
import android.util.Log;

import org.jetbrains.annotations.NotNull;

/**
 * edie create on 2019/3/26
 */
public class My1ViewModel extends ViewModel {
    private static final String TAG = "My1ViewModel";
    private int tag;
    private String hello = "测试";

    private final MutableLiveData<String> mMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getLiveData() {
        return mMutableLiveData;
    }

    public LiveData<Person> getTran() {
        return Transformations.map(mMutableLiveData, input -> new Person(1, input));
    }

    public LiveData<Person> getTran1() {
        return Transformations.switchMap(mMutableLiveData, this::getPersonMutableLiveData);
    }

    @NotNull
    private MutableLiveData<Person> getPersonMutableLiveData(String input) {
        MutableLiveData<Person> data = new MutableLiveData<>();
        data.setValue(new Person(1, input));
        Log.i(TAG, "getTran1: ");
        return data;
    }

    public void setHello() {
        mMutableLiveData.setValue(hello + ++tag);
    }
}
